package com.hx.vending.service.impl;

import com.hx.vending.bean.OnlineOrder;
import com.hx.vending.bean.UnderlineOrder;
import com.hx.vending.bean.entity.Orderdata;
import com.hx.vending.bean.entity.UnderlineOrderVo;
import com.hx.vending.dao.OnlineOrderMapper;
import com.hx.vending.service.IUnderlineOrderService;
import com.hx.vending.util.Msg;
import com.hx.vending.util.ToolUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/12/11.
 */
@Service
public class UnderlineOrderServiceImpl implements IUnderlineOrderService {
    @Resource
    private OnlineOrderMapper onlineOrderMapper;

    @Override
    public Msg getUnderlineOrderList(Map<String, Object> paramMap, Integer userId) {
        Msg returnMsg = Msg.success();
        if (ToolUtil.isEmpty(paramMap.get("pageNum")) || (Integer) paramMap.get("pageNum") <= 0) {
            return Msg.fail().add("msg", "错误的页数值");
        }
        if (ToolUtil.isEmpty(userId)) {
            return Msg.fail().add("msg", "用户信息不能为空");
        }
        //分页页数 从pageNum开始，查十条
        Integer pageNum = ((Integer) paramMap.get("pageNum") - 1) * 10;
        //添加到map中
        paramMap.put("pageNum", pageNum);
        paramMap.put("userId",userId);
        //查询数据
        List<UnderlineOrderVo> list = onlineOrderMapper.getUnderlineOrderList(paramMap);
        //添加集合到返回Msg中
        returnMsg.add("resultList", list);
        return returnMsg;
    }

    @Override
    public Msg getUnderlineOrder(Integer userId,String startTime,String endTime) {
        if (ToolUtil.isEmpty(userId)) {
            return Msg.fail().add("msg", "用户信息不能为空");
        }
        try {
            List<OnlineOrder> underlineOrders = onlineOrderMapper.selectUnderlineByUserTime(userId, startTime, endTime);
            //总金额
            Double money = onlineOrderMapper.selectUnderCountMoney(userId, startTime, endTime);
            //支付人数
            int count = onlineOrderMapper.selectUnderCountPeople(userId, startTime, endTime);
            Orderdata orderdata = new Orderdata();
            //总笔数
            orderdata.setTotalnumber(underlineOrders.size());
            orderdata.setPeople(count);
            orderdata.setTransactionMoney(money);
            return Msg.success().add("data", orderdata);
        }catch(Exception e){
            Orderdata data = new Orderdata();
            //总笔数
            data.setTotalnumber(0);
            //总人数
            data.setPeople(0);
            data.setTransactionMoney(0D);
            return Msg.success().add("data", data);
        }
    }
}
